package com.huiquan.vocab.service;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.framework.base.BaseService;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.GetListUtil;
import com.huiquan.framework.utils.RespHeaderUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.vocab.dao.VocabElementDesignateOrganDao;
import com.huiquan.vocab.domain.VocabElementClassify;
import com.huiquan.vocab.domain.VocabElementDesignateOrgan;

@Service
public class VocabElementDesignateOrganService extends BaseService {

	@Autowired
	private VocabElementDesignateOrganDao vocabElementDesignateOrganDao;
	@Autowired
	private VocabElementClassifyService vocabElementClassifyService;

	public ModelAndView list(String startStr, String elementKey, String organKey, String exactSearchFlag,
			String checkFlag,String countPerPageStr) {

		Map<String, Object> param = new HashMap<>();
		if (elementKey != null && !elementKey.isEmpty()) {
			if (exactSearchFlag != null && exactSearchFlag.equals("1")) {
				param.put("element", elementKey);
			} else {
				param.put("element", "%" + elementKey + "%");
			}
		}
		if (organKey != null && !organKey.isEmpty()) {
			if (exactSearchFlag != null && exactSearchFlag.equals("1")) {
				param.put("organ", organKey);
			} else {
				param.put("organ", "%" + organKey + "%");
			}
		}
		if (checkFlag != null && !checkFlag.isEmpty()) {
			param.put("checkFlag", checkFlag);
		}
		int totalSize = vocabElementDesignateOrganDao.retrieveSize(param);

		Map<String, Integer> pageNo ;
		if (countPerPageStr != null && !countPerPageStr.isEmpty()) {
			int countPerPage = Integer.parseInt(countPerPageStr);
			pageNo = GetListUtil.getPageNoParam(totalSize, startStr, countPerPage);
			param.put("countPerPage", countPerPage);
		} else {
			pageNo = GetListUtil.getPageNoParam(totalSize, startStr);
		}
		int endPage = pageNo.get("endPage");
		int start = pageNo.get("start");
		int startIndex = pageNo.get("startIndex");

		List<VocabElementDesignateOrgan> list = new ArrayList<>();

		if (startIndex < totalSize) {
			param.put("startIndex", startIndex);
			list = vocabElementDesignateOrganDao.retrieveList(param);
		}

		Map<String, String> showSearch = new LinkedHashMap<>();
		showSearch.put("elementKey", "小词");
		showSearch.put("organKey", "器官");

		Map<String, Object> map = GetListUtil.getReturnMap2(totalSize, start, startIndex, endPage, list, null);
		map.put("showSearch", showSearch);

		return new ModelAndView("vocab/element_index", map);
	}

	public ReturnData designateOrgan(String id, String organ) {

		VocabElementDesignateOrgan edo = vocabElementDesignateOrganDao.retrieveObjectById(Long.parseLong(id));
		edo.setOrgan(organ);

		vocabElementDesignateOrganDao.update(edo);

		return ReturnUtil.success();
	}

	public Object getOrganData() {
		List<VocabElementClassify> list = vocabElementClassifyService.getDataByCategory("");
		StringBuffer organStr = new StringBuffer();
		for (VocabElementClassify temp : list) {
			organStr.append(temp.getName());
			organStr.append(",");
		}
		if (organStr.length() > 1) {
			organStr.substring(0, organStr.length() - 1);
		}
		return organStr.toString();
	}

	public void init() {
		vocabElementDesignateOrganDao.init();
	}

	public ReturnData submitPage(String ids) {

		vocabElementDesignateOrganDao.checkByIds(ids);

		return ReturnUtil.success();
	}

	public void export(HttpServletResponse resp) throws Exception {
		RespHeaderUtil.setXlsxResp(resp, "指定器官");
		Map<String, Object> param = new HashMap<>();
		param.put("checkFlag", "1");
		// 查询出所有需要导出的数据
		List<VocabElementDesignateOrgan> elementLists = vocabElementDesignateOrganDao.retrieveList(param);

		Row row;
		Cell cell = null;

		List<String[]> exportList = new ArrayList<>();
		// 循环编辑每行
		for (VocabElementDesignateOrgan element : elementLists) {
			String[] elementAndOrgan = new String[3];
			elementAndOrgan[0] = element.getElement();
			elementAndOrgan[1] = element.getProperty();
			elementAndOrgan[2] = element.getOrgan();
			exportList.add(elementAndOrgan);
		}
		Workbook workbook = new XSSFWorkbook(); // 创建文档
		Sheet sheet = workbook.createSheet(); // 创建Sheet

		// 写入文件
		int i = 0;
		for (String[] line : exportList) {
			row = sheet.createRow(i);
			for (int j = 0; j < line.length; j++) {
				cell = row.createCell(j);
				cell.setCellValue(line[j]);
			}
			i++;
		}

		OutputStream os = resp.getOutputStream();
		workbook.write(os);

		os.close();
		workbook.close();
	}
}
